(function($, window) {
	function getQueryString(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if(r != null) {
			return unescape(r[2]);
		}
		return null;
	}
	function setToken() {
		if(getQueryString('code')) {
			window.localStorage.setItem('token', getQueryString('code'));
		} else {
			
			if(!window.localStorage.getItem('token')){
				//没有token，跳转到登录页面
				window.location = window.globalConfig.authPath;
				return;
			}
		}
		authUser();
	}

	function authUser() {
		$.ajax({
			type: 'GET',
			dataType: 'json',
			beforeSend: function(request) {
				request.setRequestHeader("Authorization", 'Bearer ' + window.localStorage.getItem('token'));
			},
			url: globalConfig.apiPath.v1 + 'api/auth/v1/users/profile',
			data: {},
			success: function(result) {
				if(result.code ===0){
					window.location = window.location.origin;
				}else{
					window.location = window.globalConfig.authPath;
				}
			},
			error:function(xhr){
				window.location = window.globalConfig.authPath;
			}
		});
	}
	function init(){
		setToken();
		
	}
	init();
})(jQuery, window)